﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;

namespace OnLineLogFileServer
{
    public class OnLineClient
    {
        private OnLineClient()
        {

        }
        static OnLineClient _intance = new OnLineClient();
        public static OnLineClient Instance
        {
            get
            {
                return _intance;
            }
        }

        public bool HasUpdate { get; set; }
        private Dictionary<string, Entry> _dic = new Dictionary<string, Entry>();

        public void Clear()
        {
            _dic.Clear();
        }
        public void Set(IPEndPoint address)
        {
            var key = address.ToString();
            if (_dic.ContainsKey(key))
            {
                goto Lable_update;
            }
            lock (_dic)
            {
                if (!_dic.ContainsKey(key))
                {
                    _dic.Add(key, new Entry() { Address = address });
                }
            }
        Lable_update:
            _dic[key].LastModifyDate = DateTime.Now;

            if (NotifyChange != null)
            {
                try
                {
                    NotifyChange(_dic);
                }
                catch
                {
                }
            }
        }

        public event Action<Dictionary<string, Entry>> NotifyChange;

        public class Entry
        {
            public IPEndPoint Address { get; set; }
            public DateTime LastModifyDate { get; set; }
        }
    }
}
